
#include "Clientstdafx.h"
#include "ClientRoot.h"
#include "math/stringConverter.h"
#include "BaseEditor.h"
using namespace Apo;
namespace Apo
{
//-----------------------------------------------------------------------------------------
void ClientRoot::LoadPropertySheets()
{
   //OGITORREGISTRATIONSTRUCT **i = m_TypeList.begin();
   for (int i= 0;i< m_TypeList.size();++i)
   {
	   if(m_TypeList.at(i)->PropertySheet != "")
	   {
		   if(m_TypeList.at(i)->PropertySheet.substr(0,1) == ".")
			   gSkinSystem->LoadPropertySheet(m_TypeList.at(i)->TypeID,m_TypeList.at(i)->PropertySheet);
		   else
			   gSkinSystem->LoadPropertySheet(m_TypeList.at(i)->TypeID,"../Plugins/" + m_TypeList.at(i)->PropertySheet);
	   }
	   i++;
   }

}
//-----------------------------------------------------------------------------------------
void ClientRoot::UnloadPropertySheets()
{
    gSkinSystem->UnLoadPropertySheets();
}
//-----------------------------------------------------------------------------------------
void ClientRoot::RecurseDeleteCustomProperties(CUSTOMPROPERTY *p)
{
    for(unsigned int i = 0;i < p->subproperties.size();i++)
    {
        RecurseDeleteCustomProperties(p->subproperties[i]);
    }
    p->subproperties.clear();
    delete p;
}
//-----------------------------------------------------------------------------------------
void ClientRoot::RecurseAddCustomProperty(CUSTOMPROPERTY *to, CUSTOMPROPERTY *from)
{
    to->PARENTID = from->PARENTID;
    to->ID = from->ID;
    to->name = from->name;
    to->type = from->type;
    to->floatformat = from->floatformat;
    to->value = from->value;
    to->lowerbound = from->lowerbound;
    to->upperbound = from->upperbound;
    to->options = from->options;
    to->restrictions.clear();

    for(unsigned int i = 0;i < from->subproperties.size();i++)
    {
	    CUSTOMPROPERTY *sub = new CUSTOMPROPERTY;
	    RecurseAddCustomProperty(sub,from->subproperties[i]);
	    to->subproperties.push_back(sub);
    }
}
//-----------------------------------------------------------------------------------------
void ClientRoot::RecurseLoadCustomProperty(TiXmlElement *e,CustomPropertyList& list)
{
    String sOptions,sRestrictions,sRestrict,sOpt;
    String sType,sLB, sUB;
    TiXmlElement* elem = e->FirstChildElement();
    if(!elem) 
        return;
    do
    {
	    CUSTOMPROPERTY *stCurrent = new CUSTOMPROPERTY;
        stCurrent->ID = 0;
	    stCurrent->floatformat = ValidAttr(elem->Attribute("floatformat"),"%.2f");
	    stCurrent->value = ValidAttr(elem->Attribute("default"),"");
	    sLB = ValidAttr(elem->Attribute("lowerbound"),"-2000000000");
	    sUB = ValidAttr(elem->Attribute("upperbound"),"2000000000");
        stCurrent->lowerbound = StringConverter::parseReal(sLB);
        stCurrent->upperbound = StringConverter::parseReal(sUB);

        sOptions = ValidAttr(elem->Attribute("options"),"");
	    sRestrictions = ValidAttr(elem->Attribute("restrictions"),"");

	    gSkinSystem->ParseStringVector(sRestrictions,stCurrent->restrictions);
	    gSkinSystem->ParseStringVector(sOptions,stCurrent->options);

        sType = ValidAttr(elem->Attribute("type"),"");
	    stCurrent->type = GetCustomPropertyID(sType);

	    if(stCurrent->type == 0) stCurrent->type = CPTYPE_STRING;

	    if(stCurrent->type >= CUSTOM_PROPERTY_TYPECOUNT)
	    {
            CUSTOMPROPERTY *inhType = 0;
            if(inhType = GetCustomProperty(sType))
	        {
	            RecurseAddCustomProperty(stCurrent,inhType);
	        }
	    }

	    stCurrent->name = ValidAttr(elem->Attribute("name"),"noname");

	    if(stCurrent->type == CPTYPE_GROUP || stCurrent->type == CPTYPE_GROUPLIST) 
            RecurseLoadCustomProperty(elem,stCurrent->subproperties);
	    list.push_back(stCurrent);
    } while (elem = elem->NextSiblingElement());
}
//-----------------------------------------------------------------------------------------
void ClientRoot::LoadCustomProperties()
{
    TiXmlDocument doc( (m_ProjectOptions.ProjectDir + "/CustomPropertySheet.xml").c_str() );
    for(unsigned int i = 0;i < m_CustomProperties.size();i++)
    {
        RecurseDeleteCustomProperties(m_CustomProperties[i]);
    }
    m_CustomProperties.clear();
    for(unsigned int i = 0;i < CUSTOM_PROPERTY_TYPECOUNT;i++)
    {
        CUSTOMPROPERTY *Dummy = new CUSTOMPROPERTY;
        Dummy->type = i;
        Dummy->ID = i;
	    Dummy->lowerbound = -2000000000;
	    Dummy->upperbound = 2000000000;
	    Dummy->floatformat = "%f";
	    Dummy->name = GetCustomPropertyTypeName(i);
	    m_CustomProperties.push_back(Dummy);
    }

    if(doc.LoadFile())
    {
        TiXmlNode* node = doc.FirstChild("CUSTOMPROPERTYDEFINITION");
        if(!node) 
            return;
	    TiXmlElement* element = node->ToElement();
	    RecurseLoadCustomProperty(element,m_CustomProperties);
    }
}
//-----------------------------------------------------------------------------------------
CUSTOMPROPERTY * ClientRoot::GetCustomProperty(String name)
{
    for(unsigned int i = 0;i < m_CustomProperties.size();i++)
    {
	    if(m_CustomProperties[i]->name == name) return m_CustomProperties[i];
    }
    return 0;
}
//-----------------------------------------------------------------------------------------
unsigned int ClientRoot::GetCustomPropertyID(String name)
{
    for(unsigned int i = 0;i < m_CustomProperties.size();i++)
    {
        if(m_CustomProperties[i]->name == name) return i;
    }
    return 0;
}
//-----------------------------------------------------------------------------------------
String ClientRoot::GetCustomPropertyTypeName(unsigned int ID)
{
    if(ID < CUSTOM_PROPERTY_TYPECOUNT)
        return CUSTOMPROPERTYTYPENAMES[ID];
    else
	    return m_CustomProperties[ID]->name;
}
//-----------------------------------------------------------------------------------------
bool ClientRoot::IsCustomTypeRestricted(StringVector &rest,unsigned int type)
{
    return false;
}
//-----------------------------------------------------------------------------------------
void ClientRoot::GetCustomPropertyList(unsigned int editortype, StringVector &result)
{
    result.clear();
    for(unsigned int i = 1;i < m_CustomProperties.size();i++)
    {
	    if(m_CustomProperties[i]->restrictions.empty() || !IsCustomTypeRestricted(m_CustomProperties[i]->restrictions,editortype))
	        result.push_back(m_CustomProperties[i]->name);
    }
}
//-----------------------------------------------------------------------------------------
}